package com.xie.system.domain.message.messagerecord.domainservice;

import com.xie.common.exception.BusinessException;
import com.xie.jpa.support.EntityOperations;
import com.xie.system.domain.message.messagerecord.domainservice.model.MessageSendModel;
import com.xie.system.domain.message.messagerecord.mapper.MessageRecordMapper;
import com.xie.system.domain.message.messagerecord.repository.MessageRecordRepository;
import com.xie.system.domain.message.messagetemplate.MessageTemplate;
import com.xie.system.domain.message.messagetemplate.repository.MessageTemplateRepository;
import com.xie.system.infrastructure.constants.MessageErrorCode;
import jodd.util.StringTemplateParser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
@Slf4j
public class MessageRecordDomainServiceImpl implements IMessageRecordDomainService{

  private final MessageTemplateRepository messageTemplateRepository;

  private final MessageRecordRepository messageRecordRepository;

  @Override
  public void sendMessage(MessageSendModel messageSendModel) {

    Optional<MessageTemplate> template = messageTemplateRepository.findByTemplateCode(
        messageSendModel.getTemplateCode());
    if(!template.isPresent()){
      throw new BusinessException(MessageErrorCode.TEMPLATE_NOT_FIND);
    }
    //模板内容替换
    String content = StringTemplateParser
        .ofMap(messageSendModel.getParams())
        .apply(template.get().getTemplate());
    EntityOperations
        .doCreate(messageRecordRepository)
        .create(() -> MessageRecordMapper.INSTANCE.model2Entity(messageSendModel))
        .update(e -> e.init(content))
        .execute();
  }
}
